home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Shareware Grab Bag
/
Shareware Grab Bag.iso
/
090
/
cmln0286.arc
/
FILETEST.C
< prev
next >
Wrap
Text File
|
1986-02-03
|
3KB
|
128 lines
/************************************************************************
* *
* filetest.c *
* *
* Example Program to test file reading and writing in C. *
* *
* This program asks for the name of an input file and output *
* file and copies from input to output. *
* *
* A. Skjellum *
* November 29, 1985 *
* *
* *
* Computer Language Magazine [C Interpreter Wrap-Up, Feb., 1986] *
* *
************************************************************************/
#include <stdio.h>
/*
#define LIVINGC 1
*/
#ifdef LIVINGC
#define fgetc getc
#define fputc putc
#endif
#define FNAME 75 /* length of file name strings */
main()
{
printf("filetest.c: copies input file to output [29-Nov-85]\n\n");
do
{
mainloop();
}
while (yesno("Another copy? "));
printf("\n\nEnd of Execution.\n");
}
mainloop()
{
int chr; /* character by character file copy */
FILE *input, *output; /* handles for input and output files */
char istring[FNAME];
char ostring[FNAME]; /* strings for file names */
input = NULL;
output = NULL; /* indicate nothing opened in either */
while (input == NULL) /* try until we get an existing file */
{
printf("\n\nEnter name of input file: ");
scanf("%s",istring);
input = fopen(istring,"r");
if (input == NULL)
printf("File not found: %s\n",istring);
}
/* now see if output file already exists: */
while (output == NULL) /* try until we get a legal output name */
{
printf("\n\nEnter name of output file: ");
scanf("%s",ostring);
if ((output = fopen(ostring,"r")) != NULL) /* exists? */
{
fclose(output); /* close it */
printf("Output file %s already exists",ostring);
if (yesno("Overwrite? ")) /* dump old file */
unlink(ostring); /* delete it */
else
{ /* start over: */
printf("Start over:");
output = NULL; /* reset loop condition */
continue;
}
}
if ((output = fopen(ostring,"w")) == NULL)
printf("Cannot open %s for output\n",ostring);
} /* end while(output == NULL) */
/* ready to copy */
while ((chr = fgetc(input)) != EOF)
fputc(chr,output);
fclose(input);
fclose(output); /* close both active files */
}
yesno(prompt) /* robust yesno input routine */
char *prompt;
{
char chr;
do /* prompt + character input: */
{
printf("\n%s",prompt); /* echo prompt */
do /* white space disposal */
{
chr = tolower(getchar()); /* lowercase it */
}
while(isspace(chr));
}
while /* data verification: */
(
(chr != 'y') && /* yes entered */
(chr != 'n') /* no entered */
);
return((chr == 'y') ? 1 : 0); /* 1 --> true, 0 --> false */
}